package org.etom.impl.ui.wizards;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.NewProjectAction;
import org.etom.connector.datatypes.IMoodleModule;
import org.etom.impl.ui.Activator;
import org.etom.impl.ui.common.IconConstants;
import org.etom.impl.ui.common.NewProjectListener;
import org.etom.impl.ui.jobs.CreateProjectJob;

public class DownloadAsWizard extends Wizard {

  private DownloadAsWizardPage wizardPage;
  private DownloadAsNewProjectPage newProjectPage;
  private IMoodleModule moodleModule;
  private Shell shell;
  private WizardDialog wizardDialog;
  
  public DownloadAsWizard(Shell shell, IMoodleModule moodleModule) {
    this.shell = shell;
    this.moodleModule = moodleModule;
    init();
  }
  
  @Override
  public boolean performFinish() {
    wizardDialog = null;
    return performNewProject();
  }

  @Override
  public void addPages() {
    if (wizardPage == null) {
      this.wizardPage = new DownloadAsWizardPage(wizardDialog, moodleModule);
    }
    if (newProjectPage == null) {
      this.newProjectPage = new DownloadAsNewProjectPage();
    }
    addPage(wizardPage);
    addPage(newProjectPage);
    setForcePreviousAndNextButtons(false);
  }

  @Override
  public IWizardPage getNextPage(IWizardPage page) {
    if (DownloadAsWizardPage.NEW_PROJECT_WIZARD == wizardPage.getNewProjectMethod()) {
      return null;
    }
    return super.getNextPage(page);
  }

  public void open() {
    if (null == wizardDialog) {
      wizardDialog = new WizardDialog(shell, this);
      wizardDialog.setBlockOnOpen(true);
    }
    wizardDialog.open();
  }
  
  private boolean performNewProject() {
    switch (wizardPage.getNewProjectMethod()) {
    case DownloadAsWizardPage.IN_EXISTING_PROJECT:
      return createInExistingProject();
    case DownloadAsWizardPage.NEW_PROJECT:
      return createNewProject();
    case DownloadAsWizardPage.NEW_PROJECT_WIZARD:
      return createNewProjectWizard();
    default:
      return false;
    }
  }

  private boolean createInExistingProject() {
    return false;
  }
  
  private boolean createNewProjectWizard() {
    NewProjectListener listener = new NewProjectListener();
    ResourcesPlugin.getWorkspace().addResourceChangeListener(
        listener, IResourceChangeEvent.POST_CHANGE);
    (new NewProjectAction(PlatformUI.getWorkbench().getActiveWorkbenchWindow())).run();
    ResourcesPlugin.getWorkspace().removeResourceChangeListener(listener);
    IProject project = listener.getProject();
    if (null == project) return false;
    CreateProjectJob job = new CreateProjectJob(project, moodleModule);
    job.setUser(true);
    job.schedule();
    /*
     * We return true here.
     * If an error is occurred in the job,
     * it will report the problem. 
     */
    return true;
  }
  
  private boolean createNewProject() {
    //TODO: if the page is not available, use default value
    String projectLocation = newProjectPage.getProjectLocation();
    String projectName = wizardPage.getProjectName();
    CreateProjectJob job =
      new CreateProjectJob(projectName, projectLocation, moodleModule);
    job.setUser(true);
    job.schedule();
    /*
     * We return true here.
     * If an error is occurred in the job,
     * it will report the problem. 
     */
    return true;
  }
  
  private void init() {
    setDefaultPageImageDescriptor(
        ImageDescriptor.createFromFile(Activator.class,
            IconConstants.WIZARD_DOWNLOAD_ICON_PATH));
    setWindowTitle(WizardsMessages.DownloadAs_Wizard_Title);
  }
  
}
